package com.example.testcomposeproject.pages.sideEffect.testRememberUpdateState

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.example.testcomposeproject.PreViewData
import kotlinx.coroutines.delay

/**
 * @author: hemin
 * @date: 2024/6/21
 * @Desc:
 *
 */
@Preview(showBackground = true, device = PreViewData.PreviewPhoneSize)
@Composable
fun PreViewTestRememberUpdateStateUI2(modifier: Modifier = Modifier) {
        TestRememberUpdateStateUI2("world")
}


/**
 * 真正的UI元素
 */
@Composable
fun TestRememberUpdateStateUI2(name: String) {

    // 使用 remember 和 mutableStateOf 来记住计数的状态
    var count by remember { mutableStateOf(0) }

    // 使用 rememberUpdatedState 来更新 currentTime lambda 的值
    val updatedTime = rememberUpdatedState(currentTime())

    Column(
        modifier = Modifier.padding(16.dp)
    ) {
        Text(text = "Hello, $name!")
        Spacer(modifier = Modifier.height(8.dp))
        Button(onClick = { count++ }) {
            Text("Increment")
        }
        Spacer(modifier = Modifier.height(8.dp))
        Text(text = "Count: $count")
        Text(text = "Current Time: ${updatedTime.value}") // 使用更新的当前时间

        Text(text = "Current Time2: ${java.text.SimpleDateFormat(
            "HH:mm:ss",
            java.util.Locale.getDefault()
        ).format(java.util.Date())}")
    }

}

@Composable
fun currentTime(): String {
    return java.text.SimpleDateFormat(
        "HH:mm:ss",
        java.util.Locale.getDefault()
    ).format(java.util.Date())
}






